/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rlpw.swing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

final class FilteredList<T> extends ArrayList<T> {

    private View<T> view;

    public FilteredList(View<T> view) {

        this.view = view;
        if (view instanceof DefaultFilter) {
            addAll(view.getDataModel());
        } else {
            List<T> ls = new ArrayList<T>();
            for (T item : view.getDataModel()) {
                if (view.getFilter().isVisible(item)) {
                    ls.add(item);
                }
            }
            addAll(ls);
        }


        if (view.getSorter() != null) {
            Collections.sort(this, view.getSorter());
        }

    }

    public int toDataModelIndex(int index) {
        return view.getDataModel().indexOf(get(index));
    }

    public int toViewIndex(int index) {
        return indexOf(view.getDataModel().get(index));
    }
}
